home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python1.4_Source / Lib / glob.py < prev    next >
Text File  |  1998-06-24  |  1KB  |  52 lines

  1. # Module 'glob' -- filename globbing.
  2.  
  3. import os
  4. import fnmatch
  5. import regex
  6.  
  7.  
  8. def glob(pathname):
  9.     if not has_magic(pathname):
  10.         if os.path.exists(pathname):
  11.             return [pathname]
  12.         else:
  13.             return []
  14.     dirname, basename = os.path.split(pathname)
  15.     if has_magic(dirname):
  16.         list = glob(dirname)
  17.     else:
  18.         list = [dirname]
  19.     if not has_magic(basename):
  20.         result = []
  21.         for dirname in list:
  22.             if basename or os.path.isdir(dirname):
  23.                 name = os.path.join(dirname, basename)
  24.                 if os.path.exists(name):
  25.                     result.append(name)
  26.     else:
  27.         result = []
  28.         for dirname in list:
  29.             sublist = glob1(dirname, basename)
  30.             for name in sublist:
  31.                 result.append(os.path.join(dirname, name))
  32.     return result
  33.  
  34. def glob1(dirname, pattern):
  35.     if not dirname: dirname = os.curdir
  36.     try:
  37.         names = os.listdir(dirname)
  38.     except os.error:
  39.         return []
  40.     result = []
  41.     for name in names:
  42.         if name[0] != '.' or pattern[0] == '.':
  43.             if fnmatch.fnmatch(name, pattern):
  44.                 result.append(name)
  45.     return result
  46.  
  47.  
  48. magic_check = regex.compile('[*?[]')
  49.  
  50. def has_magic(s):
  51.     return magic_check.search(s) >= 0
  52.